import 'package:flutter/material.dart';
import 'package:citiesocial/_include.dart';

///首页-闪购
class FlashBuy extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _FlashBuyState();
}

class _FlashBuyState extends BaseState<FlashBuy> {
  final _categoryList = List<CategoryEntity>();
  final _activityList = List<ActicityEntity>();

  var _index = 0, _page = 1;
  var _loadable = true;

  void _selectTab(int index) async {
    if (_index != index) {
      _index = index;
      _page = 1;
      _loadList(_categoryList[_index].cat_id, _page);
    }
  }

  void _load() async {//总是多一个圈，不想解决，只能粗暴地让它不转圈
    _loadable = true;
    //LoadingDialog.show(context);
    List<CategoryEntity> category =
        await HttpManager.category(context: context);
    //await LoadingDialog.dismiss(context);
    if (category != null && category.length > 0) {
      setState(() {
        _categoryList.clear();
        _categoryList.addAll(category);
      });
      _index = 0;
      _page = 1;
      _loadList(category[_index].cat_id, _page);
    }
  }

  void _loadList(id, page) async {
    LoadingDialog.show(context);
    final deviceId = await getDeviceId();
    List<ActicityEntity> list = await HttpManager.flashSalesList(id, deviceId,
        context: context, page: page);
    await LoadingDialog.dismiss(context);
    if (page <= 1) _activityList.clear();
    if (list != null && list.length > 0) {
      _loadable = list.length >= HttpManager.default_size;
      _activityList.addAll(list);
    } else {
      _loadable = false;
    }
    setState(() {});
  }

  void _loadMore() {
    if (_loadable) {
      _page++;
      _loadList(_categoryList[_index].cat_id, _page);
    }
  }

  @override
  void initState() {
    Future.delayed(
      Configs.loadDelay,
      () {
        _load();
      },
    );
    super.initState();
  }

  @override
  Widget build(BuildContext context) => Column(
        children: <Widget>[
          SingleChildScrollView(
            scrollDirection: Axis.horizontal,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: List.generate(
                _categoryList.length,
                (index) => GestureDetector(
                      child: Container(
                        color: Colors.white,
                        child: Text(
                          _categoryList[index].cat_name,
                          style: TextStyle(
                              color: this._index == index
                                  ? Configs.main
                                  : Colors.black),
                        ),
                        padding: EdgeInsets.all(15.0),
                      ),
                      onTap: () => _selectTab(index),
                    ),
              ),
            ),
          ),
          Expanded(
            child: DListView(
              itemCount: _activityList.length,
              itemBuilder: (_, index) => Container(
                    child: LongGoodsItemIndex(_activityList[index]),
                    margin: EdgeInsets.only(top: 10.0),
                  ),
              onRefresh: () async => _load(),
              onLoadmore: _loadMore,
            ),
          ),
        ],
      );
}
